﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class GroupAlbumDAL
    {
        private const string Waming = "SqlServerDAL.GroupAlbumDAL";

        public bool Save(GroupAlbumDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@GaName", SqlDbType.VarChar) {Value = dto.GaName},
                                         new SqlParameter("@GaTime", SqlDbType.DateTime) {Value = dto.GaTime},
                                         new SqlParameter("@GaCreator", SqlDbType.Int) {Value = dto.GaCreator},
                                         new SqlParameter("@GaGroupCode", SqlDbType.VarChar) {Value = dto.GaGroupCode}
                                     };
                if (dto.GaId == 0)
                {
                    sql =
                        "insert into GroupAlbum(GaName,GaTime,GaCreator,GaGroupCode) values(@GaName,@GaTime,@GaCreator,@GaGroupCode)";
                }
                else
                {
                    sql =
                        "update GroupAlbum set GaName=@GaName,GaTime=@GaTime,GaCreator=@GaCreator,GaGroupCode=@GaGroupCode where GaId=@GaId";
                    parameters.Add(new SqlParameter("@GaId", SqlDbType.Int) { Value = dto.GaId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(GroupAlbumDto dto)", ex);
                return false;
            }
        }
    }
}
